import Config from "./Config";
import {getConfig} from "./ConfigHelper";
import Context from "../../framework/Context";
import {ProfitEvent} from "../consts/Cmds";

/**
 * 玩家金币.
 */
export class UnlockSkin {
    public id: number = 0;
    public isNew: boolean = false;
    public level: number = 1;
}

export class UserProfit extends Config {

    // 金币数量.
    public coins: number = 0;

    // 钻石数量.
    public demands: number = 0;

    public lastGetDiamondTs: number = 0;
    // 已解锁的皮肤.
    private currentSkin: number = 1;

    private unlockSkins: { [key: number]: UnlockSkin } = {
        1: {id: 1, isNew: false, level: 1},
    };

    constructor() {
        super("profit");
    }

    public getCurrentSkin(): number {
        return this.currentSkin || 1;
    }

    public setCurrentSkin(skin: number) {
        if (this.currentSkin == skin) {
            return;
        }
        this.currentSkin = skin;
        Context.get().messenger.sendMessage(ProfitEvent.ON_SKIN_CHANGE, [this.currentSkin]);
    }

    public addCoin(num: number) {
        if (num <= 0) {
            return;
        }
        this.coins = Math.round(this.coins + num);

        Context.get().messenger.sendMessage(ProfitEvent.ON_COIN_CHANGE, [this.coins]);
        this.save();

    }

    public loseCoin(num: number) {
        if (num <= 0) {
            return;
        }
        if (this.coins >= num) {
            this.coins -= num;
            Context.get().messenger.sendMessage(ProfitEvent.ON_COIN_CHANGE, [this.coins]);
            this.save();
        }
    }

    public addDemand(num: number) {
        if (num <= 0) {
            return;
        }
        this.demands = this.demands || 0;
        this.demands += num;
        this.save();
        Context.get().messenger.sendMessage(ProfitEvent.ON_DIAMOND_CHANGE, [this.demands]);
    }

    public useDemand(num: number) {
        if (num <= 0) {
            return;
        }
        this.demands = this.demands || 0;
        this.demands -= num;
        if (this.demands < 0) {
            this.demands = 0;
        }
        this.save();
        Context.get().messenger.sendMessage(ProfitEvent.ON_DIAMOND_CHANGE, [this.demands]);
    }

    public setUnlockSkin(skinId: number) {
        if (!skinId) {
            console.warn("待解锁的皮肤ID异常");
            return;
        }
        if (this.unlockSkins[skinId]) {
            console.warn("皮肤已解锁,重复解锁.");
            return;
        }
        this.unlockSkins[skinId] = {id: skinId, isNew: true, level: 1};
        this.save();
        Context.get().messenger.sendMessage(ProfitEvent.ON_SKIN_UNLOCK, [skinId]);
    }

    //
    // public getUnlockSkinCrit(): number {
    //     let level = this.unlockSkins[this.currentSkin].level;
    //     return SKINS[this.currentSkin].critBase + SKINS[this.currentSkin].critRate * level;
    // }
    //
    // public getUnlockSkinCost(): number {
    //     let level = this.unlockSkins[this.currentSkin].level;
    //     return SKINS[this.currentSkin].upBase + SKINS[this.currentSkin].upRate * level;
    // }
    //
    // // 判断当前皮肤是否已到最高级了.
    // public isCurrentSkinMax(): boolean {
    //     let level = this.unlockSkins[this.currentSkin].level;
    //     return SKINS[this.currentSkin].levelLimit <= level;
    // }
    //
    // public upgradeUnlockSkin(skinId: number) {
    //     if (!skinId) {
    //         console.warn("待解锁的皮肤ID异常");
    //         return;
    //     }
    //
    //     this.unlockSkins[skinId].level++;
    //     this.save();
    //     Context.get().messenger.sendMessage(ProfitEvent.ON_SKIN_UPGRADE, [skinId]);
    //
    //     logger.sendEvent("升级皮肤", {level: this.unlockSkins[skinId].level})
    // }

    public getUnlockSkin(skinId: number): UnlockSkin {
        return this.unlockSkins[skinId];
    }
}


export const userProfit: UserProfit = getConfig(UserProfit);

